package com.epfresh.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.epfresh.R;
import com.epfresh.calendar.roomorama.caldroid.CaldroidFragment;
import com.epfresh.calendar.roomorama.caldroid.CaldroidListener;
import com.epfresh.calendar.roomorama.caldroid.CaldroidSampleCustomFragment;
import com.epfresh.calendar.roomorama.caldroid.CalendarHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/*预设 "2016-11-10" "2016-12-09" "2017-4-09"三天有货 点击弹出日期提醒*/
@SuppressLint("SimpleDateFormat")
public class CalendarPreActivity extends AppCompatActivity {
    private CaldroidFragment caldroidFragment;

    View close;

    String dateSelected;
    Date minDate,maxDate,dateSelectedDate;
    Map<Date,Integer> mapTextColor=new HashMap<Date,Integer>();
    ArrayList<String> timeStringList = new ArrayList<>();
    ColorDrawable yellow;
    private void setCustomResourceForDates() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

//      Log.i("dateSelected",dateSelected);
        Calendar cal = Calendar.getInstance();
        // Min date is today
        minDate = cal.getTime();
        // Max date is next year's today
        cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 365);
        maxDate = cal.getTime();
        Date date = new Date();
        Map<Date,Drawable> mapBackGroundColor=new HashMap<Date,Drawable>();

        yellow = new ColorDrawable(getResources().getColor(R.color.calendar_yellow));
        for(int i=0;i<timeStringList.size();i++){
            try {
                date = sdf.parse(timeStringList.get(i));
            }catch (Exception e){
                e.printStackTrace();
            }
            mapTextColor.put(date,R.color.green_little);
            mapBackGroundColor.put(date,yellow);
        }

        if (caldroidFragment != null) {

/*            //caldroidFragment.moveToDate(dateSelectedDate);
            //caldroidFragment.setCalendarDate(dateSelectedDate);
            caldroidFragment.setSelectedDate(dateSelectedDate);*/
            caldroidFragment.setTextColorForDates(mapTextColor);//可选日期字体红色
            caldroidFragment.setBackgroundDrawableForDates(mapBackGroundColor);//可选日期背景黄色
            caldroidFragment.setTextColorForDate(R.color.green_little,dateSelectedDate);
            caldroidFragment.setBackgroundDrawableForDate(getResources().getDrawable(R.drawable.green_border),dateSelectedDate);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar_pre);
        close = findViewById(R.id.tv_close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        timeStringList = getIntent().getStringArrayListExtra("timeStringList");
        int position = getIntent().getIntExtra("position",0);
        dateSelected = timeStringList.get(position);
        try {
            dateSelectedDate = formatter.parse(dateSelected);
        }catch (Exception e){
            e.printStackTrace();
        }
        caldroidFragment = new CaldroidSampleCustomFragment();

        //caldroidFragment = new CaldroidFragment();
        if (savedInstanceState != null) {
            caldroidFragment.restoreStatesFromKey(savedInstanceState,
                    "CALDROID_SAVED_STATE");
        }
        // If activity is created from fresh
        else {
            Bundle args = new Bundle();
            Calendar cal = Calendar.getInstance();
            cal.setTime(dateSelectedDate);
            //args.putInt(CaldroidFragment.START_DAY_OF_WEEK, CaldroidFragment.MONDAY); // Monday
            args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
            args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
            Map<String ,Object> extra =new HashMap<String,Object>();
            extra.put("有货",CalendarHelper.convertDateToDateTime(cal.getTime()));
            caldroidFragment.setExtraData(extra);
            args.putBoolean(CaldroidFragment.ENABLE_SWIPE, true);
            args.putBoolean(CaldroidFragment.SIX_WEEKS_IN_CALENDAR, true);
            caldroidFragment.setArguments(args);
        }

        setCustomResourceForDates();

        // Attach to the activity
        FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        t.replace(R.id.calendar1, caldroidFragment);
        t.commit();

        // Setup listener
        final CaldroidListener listener = new CaldroidListener() {

            @Override
            public void onSelectDate(Date date, View view) {

                if(mapTextColor.containsKey(date)){//当前如果是有数据的日期
                    dateSelected = formatter.format(date);
                    int position = timeStringList.indexOf(dateSelected);
                    Intent intent = new Intent();
                    intent.putExtra("position", position);
                    setResult(RESULT_OK, intent);
                    finish();
                    return;
                    //Toast.makeText(getApplicationContext(), "keyi",Toast.LENGTH_SHORT).show();
                }else{//当前日期没有数据
/*                    Toast.makeText(getApplicationContext(), formatter.format(date),
                            Toast.LENGTH_SHORT).show();*/
                }
            }

            @Override
            public void onChangeMonth(int month, int year) {
/*                String text = "month: " + month + " year: " + year;
                Toast.makeText(getApplicationContext(), text,
                        Toast.LENGTH_SHORT).show();*/
            }

            @Override
            public void onLongClickDate(Date date, View view) {
/*                Toast.makeText(getApplicationContext(),
                        "Long click " + formatter.format(date),
                        Toast.LENGTH_SHORT).show();*/
            }

            @Override
            public void onCaldroidViewCreated() {
/*                if (caldroidFragment.getLeftArrowButton() != null) {
                    Toast.makeText(getApplicationContext(),
                            "Caldroid view is created", Toast.LENGTH_SHORT)
                            .show();
                }*/
            }

        };
        caldroidFragment.setCaldroidListener(listener);
    }

    /**
     * Save current states of the Caldroid here
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        if (caldroidFragment != null) {
            caldroidFragment.saveStatesToKey(outState, "CALDROID_SAVED_STATE");
        }
    }

}
